package org.abettor.pushbox.download;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.abettor.pushbox.config.Config;
import org.abettor.pushbox.model.SelfMessage;
import org.abettor.pushbox.net.NetTools;

import android.app.Activity;

/**
 * 用来下载
 * 
 * @author jzh
 * 
 */
public class DownloadMsg {
	private NetTools netTools = new NetTools();
	private Activity context;

	public DownloadMsg(Activity context) {
		this.context = context;
	}
	/**
	 * 读取消息的列表
	 * @param nickName
	 * @param beginTime
	 * @param pageSize
	 * @return
	 */
	public String downMsgBoxStr(String nickName,int pageSize) {
		Map<String, String> map = new HashMap<String, String>();
		if (nickName != null) {
			map.put("nickName", nickName);
		}
		map.put("pageSize", String.valueOf(pageSize));
		String ret = netTools.postData(Config.listMessageBoxUrl, map);
		return ret;
	}
	/**
	 * 查询消息的列表
	 * @param nickName
	 * @param msgBoxId
	 * @param date
	 * @param pageSize
	 * @return
	 */
	public String downEarlyMsgStr(String nickName,int msgBoxId,Date date,int pageSize){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Map<String, String> map = new HashMap<String, String>();
		if(nickName != null){
			map.put("nickName", nickName);
		}
		map.put("senderId", String.valueOf(msgBoxId));
		if(date != null){
			map.put("beginTime", sdf.format(date));
		}
		map.put("pageSize", String.valueOf(pageSize));
		String ret = netTools.postData(Config.listEarlyMessageUrl, map);
		return ret;
	}
	/**
	 * 列出较新消息的列表
	 * @param nickName
	 * @param msgBoxId
	 * @param date
	 * @param pageSize
	 * @return
	 */
	public String downNewMsgStr(String nickName,int msgBoxId,Date date,int pageSize){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Map<String, String> map = new HashMap<String, String>();
		if(nickName != null){
			map.put("nickName", nickName);
		}
		map.put("senderId", String.valueOf(msgBoxId));
		if(date != null){
			map.put("beginTime", sdf.format(date));
		}
		map.put("pageSize", String.valueOf(pageSize));
		String ret = netTools.postData(Config.listNewMessageUrl, map);
		return ret;
	}
	/**
	 * 发送消息的操作
	 * @param boxId
	 * @param msg
	 * @return
	 */
	public boolean sendMsg(int boxId,String msg){
		Map<String, String> map = new HashMap<String, String>();
		map.put("receiverId", String.valueOf(boxId));
		map.put("content", msg);
		SelfMessage self = Config.readSelfMessage(context);
		map.put("nickName", self.getNickName());
		String ret = netTools.postData(Config.addMsgUrl, map);
		if(ParaDownloadMsg.isSendMsgSuccess(ret)){
			return true;
		}
		return false;
	}
}
